﻿using backend.Entity;
using backend.Enum;
using dms.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace backend.Controllers
{
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class StatisticController : Controller
    {
        private readonly MyContext _context;
        public StatisticController(MyContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 仪表盘
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("[action]")]
        public MyJsonResult Dashboard()
        {
            // 获取当前用户
            User current = _context.Users.FirstOrDefault(a => a.UserName == this.User.Identity.Name);
            IEnumerable<Project> list = null;
            if (current.Role == RoleType.Manager)
            {
                list = _context.Projects;
            }
            else
            {
                list = _context.Projects.Include(a => a.Users).Where(a => a.Users.Contains(current));
            }

            // 总项目数
            int totalProject = list.Count();

            // 未开工项目
            int waitProject = list.Count(a => a.Status==ProjectStatus.Wait);

            // 进行中项目
            int doingProject = list.Count(a => a.Status == ProjectStatus.Doing);

            // 已完结项目
            int overProject = list.Count(a => a.Status == ProjectStatus.Over);

            return MyJsonResult.Ok(new { totalProject, waitProject, doingProject, overProject });
        }

        /// <summary>
        /// 分项目状态统计
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("[action]")]
        public MyJsonResult Status(int? year = null)
        {
            // 获取当前用户
            User current = _context.Users.FirstOrDefault(a => a.UserName == this.User.Identity.Name);
            IEnumerable<Project> list = _context.Projects.Include(a => a.Users);
            if (current.Role == RoleType.Manager)
            {
                if (year.HasValue)
                {
                    list = list.Where(a => a.StartDate.Year == year.Value);
                }
            }
            else
            {
                if (year.HasValue)
                {
                    list = list.Where(a => a.StartDate.Year == year.Value && a.Users.Contains(current));
                }
                else
                {
                    list = list.Where(a => a.Users.Contains(current));
                }
            }
            var result = list.GroupBy(a => new { a.Status }).Select(a => new { status = a.Key.Status, count = a.Count() });
            return MyJsonResult.Ok(result);
        }

        /// <summary>
        /// 月度分项目状态统计
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("[action]")]
        public MyJsonResult MonthStatus(int? year = null)
        {
            // 获取当前用户
            User current = _context.Users.FirstOrDefault(a => a.UserName == this.User.Identity.Name);
            // 查询有效的任务图片
            IEnumerable<Project> all = _context.Projects.Include(a => a.Users);
            if (current.Role == RoleType.Manager)
            {
                if (year.HasValue)
                {
                    all = all.Where(a => a.StartDate.Year == year.Value);
                }
            }
            else
            {
                if (year.HasValue)
                {
                    all = all.Where(a => a.StartDate.Year == year.Value && a.Users.Contains(current));
                }
                else
                {
                    all = all.Where(a => a.Users.Contains(current));
                }
            }

            var list = all.GroupBy(c => new { c.Status, c.StartDate.Month }).Select(g => new
            {
                TypeName = g.Key.Status,
                Month = g.Key.Month,
                Qty = g.Count()
            }).ToList();

            var types = list.Select(a => a.TypeName).Distinct();
            List<MonthTypeDTO> data = new List<MonthTypeDTO>();
            foreach (var type in types)
            {
                List<int> arr = new List<int>();
                for (int i = 1; i <= 12; i++)
                {
                    if (list.Exists(a => a.TypeName == type && a.Month == i))
                    {
                        arr.Add(list.SingleOrDefault(a => a.TypeName == type && a.Month == i).Qty);
                    }
                    else
                    {
                        arr.Add(0);
                    }
                }
                data.Add(new MonthTypeDTO { type = type.ToString(), data = arr });
            }
            return MyJsonResult.Ok(data);
        }
    }
}
